/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddItem.java
 *
 * Created on Nov 5, 2011, 4:20:22 PM
 */
package mpinventory;

import java.sql.*;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
/**
 *
 * @author msi
 */
public class AddItem extends javax.swing.JPanel {

    private Connection conn;
    private Dimension size;
    private ItemQuery itemQuery;
    public int width;
    public int height;
    public int screenWidth;
    public int screenHeight;
    public boolean backToMainMenu=false;
    private String quantityUnit;
    /** Creates new form AddItem */
    public AddItem() {
        initComponents();
    }
    public AddItem(Connection conn, int screenWidth, int screenHeight) {
        this.conn=conn;
        this.screenWidth=screenWidth;
        this.screenHeight=screenHeight;
        initComponents();
        size=this.getPreferredSize();
        width=size.width;
        height=size.height+35;
        itemQuery=new ItemQuery(conn);
        txtInvAItemID.setText(itemQuery.setID()+"");
        quantityUnit=cbxInvAUnit.getSelectedItem().toString();
        cbxInvAUnit.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent actionEvent){
                quantityUnit=cbxInvAUnit.getSelectedItem().toString();
            }
        });
    }
    @Override
    public int getWidth(){
        return width;
    }
    @Override
    public int getHeight(){
        return height;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        txtInvAPrice = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtInvAQuantity = new javax.swing.JTextField();
        txtInvAItemID = new javax.swing.JTextField();
        btnInvASave = new javax.swing.JButton();
        btnInvACancel = new javax.swing.JButton();
        txtInvADescription = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        cbxInvAUnit = new javax.swing.JComboBox();

        jLabel5.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel5.setText("Unit Price:");

        txtInvAPrice.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        jLabel1.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 25));
        jLabel1.setText("Add Item");

        jLabel4.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel4.setText("Description:");

        jLabel2.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel2.setText("Item ID:");

        txtInvAQuantity.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N

        txtInvAItemID.setEditable(false);
        txtInvAItemID.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));

        btnInvASave.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnInvASave.setText("Save");
        btnInvASave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        btnInvACancel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnInvACancel.setText("Cancel");
        btnInvACancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        txtInvADescription.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel3.setText("Quantity:");

        cbxInvAUnit.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N
        cbxInvAUnit.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "roll", "length", "pcs.", "ft.", "meters", "box", "sets", "pairs", "kl.", "cans", "gal.", "pint" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtInvAItemID, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtInvAQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(cbxInvAUnit, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtInvADescription)
                            .addComponent(txtInvAPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(138, 138, 138)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(73, 73, 73)
                        .addComponent(btnInvASave, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(57, 57, 57)
                        .addComponent(btnInvACancel, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtInvAItemID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtInvADescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtInvAQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbxInvAUnit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtInvAPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnInvASave)
                    .addComponent(btnInvACancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        if(txtInvADescription.getText().equals("") && txtInvAPrice.getText().equals("") && txtInvAQuantity.getText().equals(""))
            backToMainMenu=true;
        else{
            JOptionPane pane=new JOptionPane("Are you sure you want to go back to the main menu? Please make sure that all changes have been saved.");
            Object[] options=new String[]{"Yes","No"};
            pane.setOptions(options);
            JDialog dialog = pane.createDialog(new JFrame(), "Back To Main Menu?");
            dialog.show();
            if(options[0].equals(pane.getValue())) //YES
                backToMainMenu=true;
        }
    }//GEN-LAST:event_cancelActionPerformed

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        itemQuery=new ItemQuery(conn);
        if(txtInvADescription.getText().equals("") || txtInvAPrice.getText().equals("") || txtInvAQuantity.getText().equals(""))
            JOptionPane.showMessageDialog(null, "All fields cannot be empty!");
        else if(Float.parseFloat(txtInvAPrice.getText())<0 || Float.parseFloat(txtInvAQuantity.getText())<0)
            JOptionPane.showMessageDialog(null, "No negative inputs!");
        else if(itemQuery.canAddItem(txtInvADescription.getText(), Float.parseFloat(txtInvAPrice.getText()))){
            JOptionPane pane=new JOptionPane("Are you sure you want to add this item?");
            Object[] options=new String[]{"Yes","No"};
            pane.setOptions(options);
            JDialog dialog = pane.createDialog(new JFrame(), "Confirm Item Addition");
            dialog.show();
            if(options[0].equals(pane.getValue())){
                itemQuery.addItem(itemQuery.setID(),Float.parseFloat(txtInvAQuantity.getText()),quantityUnit,txtInvADescription.getText(),Float.parseFloat(txtInvAPrice.getText()));
                backToMainMenu=true;
            } //YES
        }
        else
            JOptionPane.showMessageDialog(null,txtInvADescription.getText()  + " already exists in the database!");
    }//GEN-LAST:event_saveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnInvACancel;
    private javax.swing.JButton btnInvASave;
    private javax.swing.JComboBox cbxInvAUnit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField txtInvADescription;
    private javax.swing.JTextField txtInvAItemID;
    private javax.swing.JTextField txtInvAPrice;
    private javax.swing.JTextField txtInvAQuantity;
    // End of variables declaration//GEN-END:variables
}
